package org.imageMaker.core;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.imageMaker.io.ImageIO;
import org.imageMaker.io.ZipUtils;
import org.imageMaker.utils.Coords;
import org.xml.sax.SAXException;

public class ImageMaker {
	private ImageIO io = null;
	private Map<String, Coords> map = new HashMap<String,Coords>();
	
	
	public ImageMaker(String parFile) throws Exception{
		try {
			//PropertiesConfiguration props = new PropertiesConfiguration(propertiesFile);
			
			ZipUtils.unzipWithoutWrite(parFile);

			//1st, unzip .par file in a temp path
			
			//2nd, load .png file
			io = ZipUtils.getImagen();
			//3rd, parse .gpd file			
		
			map = ZipUtils.getCoordenadas();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw e;
		} catch (SAXException e) {
			e.printStackTrace();
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
	}
	

	
	public void surroundNode(String nodeName){
		System.out.println(nodeName);
		Coords coords = map.get(nodeName);
		System.out.println(coords);
		Graphics2D g2d = io.getBufIm().createGraphics();

		// Draw on the image
		g2d.setColor(Color.red);
		g2d.setStroke(new BasicStroke(5F));
		g2d.draw(new Rectangle2D.Float(	new Float(coords.getX()), 
										new Float(coords.getY()),
										new Float(coords.getWidth()), 
										new Float(coords.getHeight())));
		g2d.dispose();
	}
	
	public void saveFile(String destJPGPath) throws Exception{
		try {
			io.saveImage(destJPGPath);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public BufferedImage getImage(){
			return io.getBufIm();
	}

	public static void main (String [] args){
		try {
			ImageMaker im = new ImageMaker("D:/alfa-ccp-retirada-proceso.par");
			im.surroundNode("Notificacion");
			im.saveFile("D:/q.jpg");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
